package com.easy.dn.ui.main.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.easy.dn.app.AppConstant;
import com.easy.dn.bean.JobAddressBean;

import java.util.List;

import dn.easy.com.easyto.R;
import se.emilsjolander.stickylistheaders.StickyListHeadersAdapter;

/**
 * 地点选择 适配器
 * Created by 丁宁
 * on 2017/7/21.
 */

public class AddressTypeAdapter extends BaseAdapter implements StickyListHeadersAdapter {
    private int type = 0;
    private LayoutInflater mInflater;
    private List<JobAddressBean> mList;

    public AddressTypeAdapter(Context context, List<JobAddressBean> list) {
        mInflater = LayoutInflater.from(context);
        this.mList = list;
    }

    @Override
    public View getHeaderView(int position, View convertView, ViewGroup parent) {
        HeaderViewHolder holder;
        if (convertView == null) {
            holder = new HeaderViewHolder();
            convertView = mInflater.inflate(R.layout.item_header, parent, false);
            holder.mAddressTitleHeader = (TextView) convertView.findViewById(R.id.tvHeader);
            convertView.setTag(holder);
        } else {
            holder = (HeaderViewHolder) convertView.getTag();
        }
        if (AppConstant.CHINA.equals(mList.get(position).getIsWhare())) {
            holder.mAddressTitleHeader.setText(AppConstant.ADDRESSALL);
        } else if (AppConstant.ADDRESSHOT.equals(mList.get(position).getIsWhare())) {
            holder.mAddressTitleHeader.setText(AppConstant.ADDRESSHOT);
        } else if (AppConstant.ADDRESSCHINACITY.equals(mList.get(position).getIsWhare())) {
            String chinaCityName = mList.get(position).getSpelling().substring(0, 1);
            holder.mAddressTitleHeader.setText(chinaCityName.toUpperCase());
        } else {
            holder.mAddressTitleHeader.setText(AppConstant.ADDRESSOTHER);
        }
        return convertView;
    }

    public void notifyData(List<JobAddressBean> list){
        this.mList = list;
        notifyDataSetChanged();
    }
    @Override
    public long getHeaderId(int position) {
        //数据有问题 强行区分header DnUtils 获取地点添加isWhare 字段 用于区分类别
        if (AppConstant.CHINA.equals(mList.get(position).getIsWhare())) {
            return 1;
        } else if (AppConstant.ADDRESSHOT.equals(mList.get(position).getIsWhare())) {
            return 2;
        } else if (AppConstant.ADDRESSCHINACITY.equals(mList.get(position).getIsWhare())) {
            return mList.get(position).getSpelling().substring(0, 1).toLowerCase().hashCode();
        } else {
            return 3;
        }
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int i) {
        return mList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item_profession, viewGroup, false);
            holder.mAddressTitle = (TextView) convertView.findViewById(R.id.title);
            holder.mCheckBox = (CheckBox) convertView.findViewById(R.id.title_check);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        JobAddressBean jobAddressBean = mList.get(i);
        holder.mAddressTitle.setText(jobAddressBean.getName());
        holder.mCheckBox.setChecked(jobAddressBean.getIsChecked());
        return convertView;
    }

    class HeaderViewHolder {
        TextView mAddressTitleHeader;
    }

    class ViewHolder {
        TextView mAddressTitle;
        CheckBox mCheckBox;
    }
}
